package cn.dglydrpy.study.j2ee.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.dglydrpy.study.j2ee.manage.dao.UserDAO;
import cn.dglydrpy.study.j2ee.manage.entity.User;

/*
 * 返回给浏览器当前用户列表
 */
public class ListUserServlet3 extends HttpServlet{
	
	private static final long serialVersionUID = 1L;

	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=utf8");
		PrintWriter out = response.getWriter();

		try {
			UserDAO dao = new UserDAO();
			List<User> users = dao.findAll();
			//将查询的信息按html的格式发送给浏览器
			out.println("<table border='1' cellspacing='0' cellpadding='0'>");
			out.println("<tr><th>编号</th><th>用户名</th><th>邮箱</th><th>电话</th><th>操作</th></tr>");
			for (User user : users) {
				out.println("<tr><th>"+user.getId()+"</th><th>"+user.getUsername()+"</th><th>"+user.getEmail()+"</th><th>"+user.getPhone()+"</th><th><a href='delUser3?id="+user.getId()+"'>删除</a></th></tr>");
			}
			out.println("</table>");
			out.println("<p><a href='addUser3.html'>添加用户</a></p>");
		} catch (Exception e) {
			e.printStackTrace();
			out.println("系统繁忙，请稍后再试！");
		}
	}
}
